//  

#region

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using pushbomb.composite.core;

#endregion

namespace pushbomb.composite.widgetmodel
{
    /// <summary>
    /// 
    /// </summary>
    public class WidgetModelPresenter : IPresenter
    {
        private readonly IRegionManager _rm;
        private readonly SubscriptionToken _sub;
        private readonly IUnityContainer _uc;


        /// <summary>
        /// Initializes a new instance of the <see cref="WidgetModelPresenter"/> class.
        /// </summary>
        /// <param name="ea">The ea.</param>
        /// <param name="rm">The rm.</param>
        /// <param name="uc">The uc.</param>
        public WidgetModelPresenter(IEventAggregator ea, IRegionManager rm, IUnityContainer uc)

        {
            _rm = rm;
            _uc = uc;
            var e = ea.GetEvent<ValueIntegerChangedEvent>();


            if (_sub != null)
                e.Unsubscribe(_sub);

            _sub = e.Subscribe(PopulateViewRegion);
        }

        #region IPresenter Members

        /// <summary>
        /// Populates the view region.
        /// </summary>
        /// <param name="count">The count.</param>
        public void PopulateViewRegion(int count)
        {
 
               foreach (var item in Enumerable.Range(0, count))
            {
                _rm.RegisterViewWithRegion("MainRegion", 
                    () => _uc.Resolve<IViewModel>("widgetVM").View);
            }
        }

        #endregion

        private static UIElement GetMedia(String source)
        {
            var m =
                new MediaElement
                    {
                        Source = new Uri(source, UriKind.Absolute),
                        AutoPlay = false,
                        Stretch = Stretch.UniformToFill,
                    };
            m.MouseEnter += (sender, args) => m.Play();
            m.MouseLeave += (sender, args) => m.Pause();

            return m;
        }

        /// <summary>
        /// Resolvers the specified i.
        /// </summary>
        /// <param name="i">The i.</param>
        /// <returns></returns>
        //public DependencyObject Resolver(int i)
        //{
        //    var g = new Grid();
        //    if (!(i > _links.Length - 1))
        //    {
        //        g.Children.Add(_funcCreate.Compile()());
        //        g.Children.Add(GetMedia(_links[i]));
        //    }
        //    else if (Math.IEEERemainder(i, 2d) == 0d)
        //    {
        //        var l = new LoadingAnimation
        //                    {
        //                        AutoPlay = true,
        //                        EllipseFill = new SolidColorBrush().WithRandomColor(),
        //                        EllipseStroke = new SolidColorBrush().WithRandomColor()
        //                    };
        //        g.Children.Add(l);
        //    }
        //    else
        //        g.Children.Add(_funcCreate.Compile()());
        //    return g;
        //}
     
    }
}